import java.io.*;
import java.text.SimpleDateFormat;
import java.util.*;

public class Customer {
    String id;
    String pw;
    ArrayList<String> shopCar = new ArrayList<>();
    ArrayList<String> record = new ArrayList<>();
    public void start(){
        System.out.println("~~~~~登录系统~~~~~");
        System.out.println("1.登录");
        System.out.println("2.注册");
        System.out.println("3.退出系统");
        Scanner scanner = new Scanner(System.in);
        int choice = scanner.nextInt();
        if(choice < 1 || choice > 3){
            System.out.println("请输入1~3的整数");
            choice = scanner.nextInt();
        }
        switch (choice){
            case 1:
                enter();
                break;
            case 2:
                register();
                break;
            case 3:
                System.out.println("谢谢使用，再见！");
        }
    }
    public void register(){
        System.out.println("~~~~~注册系统~~~~~");
        System.out.println("请输入分别输入手机号和密码,以及用户名和邮箱地址");
        Scanner scanner = new Scanner(System.in);
        String a,b,c,d;
        a = scanner.next();b = scanner.next();
        c = scanner.next();d = scanner.next();
        Date date = new Date();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String time =dateFormat.format(date);
        File file = new File("src/Customer-data");
        try {
            BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(file,true));
            bufferedWriter.write(a + " " + b + " " + c + " " + "铜牌用户" + " " + time + " " + "0" + " " + d + "\n");
            bufferedWriter.close();
        }catch (IOException e){
            System.out.println(e.toString());
        }
        System.out.println("注册成功，请登录");
        enter();
    }
    public void enter(){
        System.out.println("~~~~~登录系统~~~~~");
        System.out.println("请输入账号");
        Scanner scanner = new Scanner(System.in);
        this.id = scanner.next();
        System.out.println("请输入密码");
        this.pw = scanner.next();
        File file = new File("src/Customer-data");
        try {
            BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
            String str = bufferedReader.readLine();
            int x = 0;
            while (str != null){
                String[] account = str.split(" ");
                str = bufferedReader.readLine();
                if(account[0].equals(id) &&account[1].equals(pw)){
                    x++;
                    System.out.println("登录成功，欢迎您,用户："+account[2]);
                    menu();
                    break;
                }
            }
            if(x!=1){
                System.out.println("账号或者密码错误，请重新登录");
                enter();
            }
            bufferedReader.close();
        }catch (IOException e){
            System.out.println(e.toString());
        }
    }
    public void menu(){
        System.out.println("~~~~~购物管理系统~~~~~");
        System.out.println("1.密码管理");
        System.out.println("2.购物管理");
        System.out.println("3.登出系统");
        Scanner scanner = new Scanner(System.in);
        int choice = scanner.nextInt();
        if(choice < 1 || choice > 3){
            System.out.println("请输入1~3的整数");
            choice = scanner.nextInt();
        }
        switch (choice){
            case 1:
                passwordManage();
                break;
            case 2:
                shoppingManage();
                break;
            case 3:
                System.out.println("退出成功！");
                break;
        }
    }
        public void passwordManage(){
            System.out.println("~~~~~密码修改系统~~~~~");
            System.out.println("请选择：1.修改密码 2.返回上一级");
            Scanner scanner = new Scanner(System.in);
            int choice = scanner.nextInt();
            if(choice < 1 || choice > 2) {
                System.out.println("请输入1~2的整数");
                choice = scanner.nextInt();
            }
            switch (choice){
                case 1:
                    passwordChange();
                    break;
                case 2:
                    menu();
                    break;
            }
        }
                public void passwordChange(){
                    System.out.println("请输入原密码");
                    Scanner scanner = new Scanner(System.in);
                    String s = scanner.next();
                    if(this.pw.equals(s)){
                        System.out.println("请输入新密码");
                        String str = scanner.next();
                        File file = new File("src/Customer-data");
                        File temp = new File("src/temp1");
                        try {
                            BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
                            BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(temp,true));
                            String string = bufferedReader.readLine();
                            while(string != null){
                                String[] account = string.split(" ");
                                if(account[0].equals(this.id)){
                                    account[1]=str;
                                }
                                String x = "";
                                for(String y:account){
                                    x += y+" ";
                                }
                                bufferedWriter.write(x+"\n");
                                string = bufferedReader.readLine();
                            }
                            bufferedReader.close();
                            bufferedWriter.close();
                            new File("src/Customer-data").delete();
                            new File("src/temp1").renameTo(file);
                            System.out.println("修改成功，请重新登录");
                            enter();
                        }catch (IOException e){
                            System.out.println(e.toString());
                        }
                    }
                }
        public void shoppingManage(){
            System.out.println("~~~~~购物管理系统~~~~~");
            Scanner scanner = new Scanner(System.in);
            System.out.println("1.添加商品到购物车");
            System.out.println("2.将商品移除购物车");
            System.out.println("3.修改商品的购买数量");
            System.out.println("4.商品的付款");
            System.out.println("5.查看购物历史");
            System.out.println("6.返回上一级");
            int choice = scanner.nextInt();
            if(choice < 1 || choice > 6){
                System.out.println("请输入1~6的整数");
                choice = scanner.nextInt();
            }
            switch (choice){
                case 1:
                    goodsCarAdd();
                    break;
                case 2:
                    goodsCarDelete();
                    break;
                case 3:
                    goodsChangeNum();
                    break;
                case 4:
                    goodsPay();
                    break;
                case 5:
                    payRecord();
                    break;
                case 6:
                    menu();
                    break;
            }
        }
                public void goodsCarAdd(){
                    File file = new File("src/goods-data");
                    try{
                        BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
                        String str = bufferedReader.readLine();
                        while (str != null){
                            String[] data = str.split(" ");
                            System.out.println("商品id:"+data[0]+" 商品名:"+data[1]+" 生产厂家:"+data[2]+" 生产日期:"+data[3]+" 零售价:"+data[5]+" 数量:"+data[6]);
                            str = bufferedReader.readLine();
                        }
                        bufferedReader.close();
                    }catch (IOException e){
                        System.out.println(e.toString());
                    }
                    Scanner scanner = new Scanner(System.in);
                    System.out.println("请输入要添加的商品：");
                    String s = scanner.next();
                    System.out.println("请输入要添加的数量：");
                    String num = scanner.next();
                    this.shopCar.add(s+" "+num);
                    System.out.println("添加完成，即将返回上一级");
                    shoppingManage();
                }
                public void goodsCarDelete(){
                    ListIterator<String> iterator = this.shopCar.listIterator();
                    while (iterator.hasNext()) {
                        String item = iterator.next();
                        String[] ban = item.split(" ");
                        System.out.println("商品名："+ban[0]+"数量："+ban[1]);
                    }
                    Scanner scanner = new Scanner(System.in);
                    System.out.println("请输入要删除的商品：");
                    String s = scanner.next();
                    Iterator<String> iterator1 = this.shopCar.iterator();
                    while (iterator1.hasNext()) {
                        String item = iterator1.next();
                        String[] ban = item.split(" ");
                        if (ban[0].equals(s)) {
                            iterator1.remove();
                        }
                    }
                    System.out.println("删除成功，即将返回上一级");
                    shoppingManage();
                }
                public void goodsChangeNum(){
                    ListIterator<String> iterator = this.shopCar.listIterator();
                    while (iterator.hasNext()) {
                        String item = iterator.next();
                        String[] ban = item.split(" ");
                        System.out.println("商品名："+ban[0]+"数量："+ban[1]);
                    }
                    Scanner scanner = new Scanner(System.in);
                    System.out.println("请输入要修改的商品名：");
                    String s = scanner.next();
                    System.out.println("请输入要修改后的商品数量：");
                    String num = scanner.next();
                    ListIterator<String> iterator1 = this.shopCar.listIterator();
                    while (iterator1.hasNext()) {
                        String item = iterator1.next();
                        String[] ban = item.split(" ");
                        if (ban[0].equals(s)) {
                            iterator1.set(s+" "+num);
                        }
                    }
                    System.out.println("修改完成，即将返回上一级");
                    shoppingManage();
                }
                public void goodsPay(){
                    ListIterator<String> iterator = this.shopCar.listIterator();
                    while (iterator.hasNext()) {
                        String item = iterator.next();
                        String[] ban = item.split(" ");
                        System.out.println("商品名："+ban[0]+"数量："+ban[1]);
                    }
                    Scanner scanner = new Scanner(System.in);
                    System.out.println("请输入要购买的商品名：");
                    String s = scanner.next();
                    File file = new File("src/goods-data");
                    try {
                        BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
                        String string = bufferedReader.readLine();
                        while(string != null){
                            String[] data = string.split(" ");
                            if(data[1].equals(s)){
                                System.out.println("1.商品id:"+data[0]+" 2.商品名:"+data[1]+" 3.生产厂家:"+data[2]+" 4.生产日期:"+data[3]+" 5.零售价:"+data[5]+" 6.数量:"+data[6]);
                            }
                            string = bufferedReader.readLine();
                        }
                        bufferedReader.close();
                    }catch (IOException e){
                        System.out.println(e.toString());
                    }
                    System.out.println("请输入要购买的数量：");
                    String num = scanner.next();
                    this.record.add(s+" "+num);
                    ListIterator<String> iterator1 = this.shopCar.listIterator();
                    while (iterator1.hasNext()) {
                        String item = iterator1.next();
                        String[] ban = item.split(" ");
                        if (ban[0].equals(s)) {
                            int a = Integer.parseInt(ban[1]) - Integer.parseInt(num) ;
                            String sum = Integer.toString(a);
                            iterator1.set(s+" "+a);
                            iterator1.remove();
                        }
                    }
                    file = new File("src/goods-data");
                    File temp = new File("src/temp");
                    try {
                        BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
                        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(temp,true));
                        String string = bufferedReader.readLine();
                        while(string != null){
                            String[] account = string.split(" ");
                            if(account[1].equals(s)){
                                int a = Integer.parseInt(account[6]) - Integer.parseInt(num) ;
                                account[6] = Integer.toString(a);
                            }
                            String x = "";
                            for(String y:account){
                                x += y+" ";
                            }
                            bufferedWriter.write(x+"\n");
                            string = bufferedReader.readLine();
                        }
                        bufferedReader.close();
                        bufferedWriter.close();
                        new File("src/goods-data").delete();
                        new File("src/temp").renameTo(file);
                    }catch (IOException e){
                        System.out.println(e.toString());
                    }
                    System.out.println("购买成功，即将返回上一级");
                    shoppingManage();
                }
                public void payRecord(){
                    ListIterator<String> iterator = this.record.listIterator();
                    while (iterator.hasNext()) {
                        String item = iterator.next();
                        String[] ban = item.split(" ");
                        System.out.println("商品名："+ban[0]+"数量："+ban[1]);
                    }
                    System.out.println("即将返回上一级");
                    shoppingManage();
                }

}
